var data_obj = {
  a: 0,
  b: 0,
  c: 0,
  onoff: 0,
};
(function () {
  var address = "http://localhost:81/html/load.html";
  var url = "http://localhost:81/api";
  $("#btn1").on("click", function () {
    if (data_obj.a === 1 && data_obj.b === 1 && data_obj.c === 1) {
      data_obj.onoff = 1;
    } else {
      data_obj.onoff = 0;
    }
    if (!data_obj.onoff) {
      return;
    } else {
      $.ajax({
        type: "post",
        url: url,
        data: {
          type: "register",
          user: $("#user").val(),
          pass: $("#pass1").val(),
        },
        success: (res) => {
          console.log(res);
          let rest = JSON.parse(res);
          if (rest.code === 200) {
            setTimeout(() => {
              window.location.href = address;
            }, 3000);
            alert("注册成功，3秒后跳到登录界面");
          }
          if (rest.code === 1) {
            $("#user").next().text(rest.msg)
          }
        },
      });
    }
  });
})();

//渲染页尾
(function () {
  $(".register_footer").load(
    "http://localhost:81/html/public.html .footer_wrap"
  );
  $(".register_bottom").load(
    "http://localhost:81/html/public.html .bottom_wrap"
  );
})();
//表单验证
(function () {
  var reg1 = new RegExp(/^[0-9a-zA-Z]\w{7,15}$/);
  var str1 = "";
  $("#user").on("input", function () {
    str1 = $("#user").val();
    if (!reg1.test(str1)) {
      $("#user").next().text("请以数字字母开头，8到16位");
      data_obj.a = 0;
    } else {
      $("#user").next().text("");
      data_obj.a = 1;
    }
  });

  var reg2 = new RegExp(/^\w{8,16}$/);
  var str2 = "";
  $("#pass1").on("input", function () {
    str2 = $("#pass1").val();
    if (!reg2.test(str2)) {
      $("#pass1").next().text("密码8到16位");
      data_obj.b = 0;
    } else {
      $("#pass1").next().text("");
      data_obj.b = 1;
    }

    if ($("#pass2").val() === $("#pass1").val()) {
      $("#pass2").next().text("");
      data_obj.c = 1;
    } else {
      $("#pass2").next().text("请输入相同的密码");
      data_obj.c = 0;
    }
  });

  $("#pass2").on("input", function () {
    if ($("#pass2").val() === $("#pass1").val()) {
      $("#pass2").next().text("");
      data_obj.c = 1;
    } else {
      $("#pass2").next().text("请输入相同的密码");
      data_obj.c = 0;
    }
  });
})();